package org.kernely.ui.utils;

import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;

public class KernelyDialogWindow extends Window{

	//Images
	public static final String ERROR_ICON = "img/error_icon32x32.png";
	public static final String WARNING_ICON = "img/warning_icon32x32.png";
	public static final String INFO_ICON = "img/info_icon32x32.png";
	public static final String ASKING_ICON = "img/asking_icon32x32.png";
	
	private static final long serialVersionUID = -2199193031990618543L;
	private Label textLabel;
	private Embedded image;
	private Button confirmButton;
	private Button cancelButton;
	private HorizontalLayout textLayout;
	private HorizontalLayout buttonsLayout;
	private GridLayout generalLayout;
	
	/**
	 * Creates a custom dialog window.
	 * @param title The title of the window.
	 * @param text The text of the window.
	 * @param icon The icon displayed in the window. Use KernelyDialogWindow static constants.
	 */
	public KernelyDialogWindow(String title, String text, String icon){
		ThemeResource resource = new ThemeResource(icon);
		this.setCaption(title);
		this.image = new Embedded("", resource);
		this.buttonsLayout = new HorizontalLayout();
		this.generalLayout = new GridLayout();
		this.textLayout = new HorizontalLayout();
		this.confirmButton = new Button("Confirm");
		this.cancelButton = new Button("Cancel");
		this.buttonsLayout.addComponent(confirmButton);
		this.buttonsLayout.addComponent(cancelButton);
		this.buttonsLayout.setSpacing(true);
		this.buttonsLayout.setSizeFull();
		this.textLabel = new Label(text);
		this.textLabel.setSizeUndefined();
		this.textLayout.addComponent(textLabel);
		this.textLayout.setComponentAlignment(textLabel, Alignment.MIDDLE_CENTER);
		this.textLayout.setSizeFull();
		this.addComponent(this.generalLayout);
		this.generalLayout.setColumns(2);
		this.generalLayout.setRows(2);
		this.generalLayout.addComponent(image,0,0);
		this.generalLayout.addComponent(textLayout,1,0);
		this.generalLayout.addComponent(buttonsLayout,0,1,1,1);
		this.generalLayout.setSpacing(true);
		this.generalLayout.setSizeFull();
		
		// Alignment of the components
		this.generalLayout.setComponentAlignment(textLayout, Alignment.MIDDLE_CENTER);
		this.generalLayout.setComponentAlignment(buttonsLayout, Alignment.MIDDLE_CENTER);
		this.buttonsLayout.setComponentAlignment(this.confirmButton, Alignment.MIDDLE_CENTER);
		this.buttonsLayout.setComponentAlignment(this.cancelButton, Alignment.MIDDLE_CENTER);
		this.setModal(true);
		this.getContent().setSizeUndefined();
		this.setResizable(false);
		
		
		this.cancelButton.addListener(new Button.ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				cancel();
			}
		});
	}

	/**
	 * Gets the confirmButton. Use this method to add click a listener for example.
	 * @return the confirmButton
	 */
	public final Button getConfirmButton() {
		return confirmButton;
	}

	/**
	 * Gets the cancelButton. Use this method to add click a listener for example.
	 * @return the cancelButton
	 */
	public final Button getCancelButton() {
		return cancelButton;
	}

	/**
	 * The default behaviour of the cancel button : remove the window.
	 */
	final private void cancel(){
		this.getParent().removeWindow(this);

	}

	/**
	 * Sets the caption of the confirm button
	 * @param text The text to set as a c. Default is "Confirm".
	 */
	public final void setConfirmButtonCaption(String caption) {
		this.confirmButton.setCaption(caption);
	}

	/**
	 * Sets the caption of the cancel button
	 * @param text The text to set. Default is "Cancel".
	 */
	public final void setCancelButtonCaption(String caption) {
		this.cancelButton.setCaption(caption);
	}
	
	
}